<?php

/**
 * This is the model class for table "user".
 *
 * The followings are the available columns in table 'user':
 * @property string $user_id
 * @property string $name
 * @property string $surname
 * @property string $birthdate
 * @property string $city
 * @property string $mobile_phone
 * @property string $home_phone
 * @property string $icq
 * @property string $home_site
 * @property string $about
 * @property string $email
 * @property string $password
 * @property string $reg_date
 * @property string $avatar
 *
 * The followings are the available model relations:
 * @property Blogpost[] $blogposts
 * @property Friend[] $friends
 * @property Friendlist[] $friendlists
 * @property Message[] $messages
 * @property Pet[] $pets
 * @property Photocomment[] $photocomments
 * @property Userphotoalbum[] $userphotoalbums
 */
class User extends CActiveRecord
{
        public $avatar;
	/**
	 * Returns the static model of the specified AR class.
	 * @return User the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'user';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('name, surname, email, password', 'required'),
			array('name, surname, city, mobile_phone, home_phone, icq', 'length', 'max'=>25),
			array('email', 'length', 'max'=>50),
                        array('email', 'email'),
                        array('home_site', 'length', 'max'=>200),
			array('password', 'length', 'max'=>32, 'min'=>6),
			array('about, reg_date, birthDate' , 'safe'),
                        array('birthdate', 'validateDate'),
			array('user_id, name, surname, birthdate, city, mobile_phone, home_phone, icq, home_site, about, email, password, reg_date, avatar_id', 'safe', 'on'=>'search'),
		);
	}

        public function validateDate()
        {
            if (!strtotime($this->birthdate))
            {
                $this->birthdate = null;
            }
        }
	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'blogposts' => array(self::HAS_MANY, 'Blogpost', 'user_id'),
			'friends' => array(self::HAS_MANY, 'Friend', 'user_id'),
			'friendlists' => array(self::HAS_MANY, 'Friendlist', 'user_id'),
			'messages' => array(self::HAS_MANY, 'Message', 'from_user'),
			'pets' => array(self::HAS_MANY, 'Pet', 'user_id'),
			'photocomments' => array(self::HAS_MANY, 'Photocomment', 'user_id'),
			'userphotoalbums' => array(self::HAS_MANY, 'Userphotoalbum', 'user_id'),
                        'avatar' => array(self::HAS_ONE, 'Uploadimage', 'image_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'user_id' => 'Пользователь',
			'name' => 'Имя',
			'surname' => 'Фамилия',
			'birthdate' => 'День рождения',
			'city' => 'Город',
			'mobile_phone' => 'Мобильный телефон',
			'home_phone' => 'Домашний телефон',
			'icq' => 'ICQ',
			'home_site' => 'Личный сайта',
			'about' => 'О себе',
			'email' => 'E-mail',
			'password' => 'Пароль',
			'reg_date' => 'Дата регистрации',
                        'avatar_id'    => 'Фото'
		);
	}

	/**
	 * Checks if the given password is correct.
	 * @param string the password to be validated
	 * @return boolean whether the password is valid
	 */
	public function validatePassword($password)
	{
		return $this->hashPassword($password)===$this->password;
	}

	/**
	 * Generates the password hash.
	 * @param string password
	 * @param string salt
	 * @return string hash
	 */
	public function hashPassword($password)
	{
		return md5($password);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('user_id',$this->user_id,true);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('surname',$this->surname,true);
		$criteria->compare('birthdate',$this->birthdate,true);
		$criteria->compare('city',$this->city,true);
		$criteria->compare('mobile_phone',$this->mobile_phone,true);
		$criteria->compare('home_phone',$this->home_phone,true);
		$criteria->compare('icq',$this->icq,true);
		$criteria->compare('home_site',$this->home_site,true);
		$criteria->compare('about',$this->about,true);
		$criteria->compare('email',$this->email,true);
		$criteria->compare('password',$this->password,true);
		$criteria->compare('reg_date',$this->reg_date,true);
                $criteria->compare('avatar',$this->avatar,true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}                
}